'''
Created on Jul 20, 2012

@author: thomas_dao
'''

from web import base
from web.base import BaseHandler
from web import urls
from kid import util
import json
import logging
from google.appengine.ext import ndb
from entity.entity import Comment
from app import article_async, comment_async

class CreateHandler(BaseHandler):
    @ndb.synctasklet
    @base.user_required
    def post(self):
        # Get input data
        content = self.request.params.get('content')
        url = self.request.params.get('url')
        
        # Create new entity
        comment = Comment()
        comment.content = content
        comment.url = url
        comment.user = util.get_current_user_key()
        comment.put()
        
        comment_dict = yield util.ndb_to_dict_async(comment, detail=["user"])
        
        self.response.write(json.dumps(comment_dict))

class ReadHandler(BaseHandler):
    def get(self):
        pass    

class UpdateHandler(BaseHandler):
    def post(self):
        pass
    

class DeleteHandler(BaseHandler):
    def post(self):
        pass

class CommentUrlHandler(BaseHandler):
    @ndb.synctasklet
    def get(self):
        url = self.request.params.get('url')
        (comments, next_curs, more) = yield comment_async.get_all(url=url)
        comments_list = yield util.ndb_to_list_async(comments, detail=['user'])
        self.response.write(json.dumps(comments_list))

app = urls.app

def main():
    app.run()

if __name__ == '__main__':
    main()